$(document).ready(function(){
	getdishes(null,null); 
});
//定义数组
var arr1 = new Array();

function addArr(menuId,dish,dishPrice){
	arr1.push(new Parent(menuId,dish,dishPrice));
}
/*
 * 查看本地添加的菜单
 */
function foreachArr(){
	$("#after_right").html('');
	$("#after_buttom").html('');
	if(arr1.length<=0){
		alert("你还未添加菜单！");
	}else{
		var $after=$("#after_right");
		var tableTop="<table class='table table-condensed'><thead><tr><th>序号</th><th>菜单名</th>"
		+"<th>价格</th><th>状态</th></tr></thead><tbody>";
		var  tableButten=" </tbody></table>";
		$after.append(tableTop);
		var tableEach=null;
		for(var i=0;i<arr1.length;i++){
		tableEach="<tr><th scope='row'>"+(i+1)+"</th><td>"+ arr1[i].dish +"</td><td>"+ arr1[i].dishPrice +"</td><td><p>菜单已添加</p></td></tr>";
		$after.append(tableEach);
		}
		$after.append(tableButten);
		var after_buttom="<button type='button' class='btn btn-success' onclick='actionbut()'>完成点菜</button>";
		$("#after_buttom").append(after_buttom);
		
	}
}


function dishesLoginOut() {
	$.getJSON('/loginOut',function (data){
		if(data=="loginOut"){
			window.location.href="/jsp/loginOut.jsp";
		}else{
			alert("你点的菜单正在处理中，还不能退出！");
		}
	});
}

function actionbut(){
	var addArr = new Array();
	for(var i=0;i<arr1.length;i++){
	addArr.push( (arr1[i].menuId));
	  }
	$.getJSON('/setDishesList',{"array[]":addArr},function (data){
		if(data=="suress"){
			alert("添加成功");
			arr1.length=0;
			foreachArr();
		}else{
			alert("提交失败");
		}
	});
}

function Parent(menuId,dish,dishPrice){  
    this.menuId=menuId;  
    this.dish=dish;  
    this.dishPrice=dishPrice;  
    this.sayName=function(){alert(this.menuId,this.dish,this.dishPrice)
    }
};
/*
 * 条件查询和分页
 */
function getdishes(menuTypeId,currentPageNo){
		var TypeId=menuTypeId;
		var PageNo=currentPageNo;
		var PageNo=null
		if(currentPageNo==null||currentPageNo==0){
			PageNo=1;
		}else{
			PageNo=currentPageNo;
		}
		$.getJSON('/dishes/getDishesList',{"menuTypeId": TypeId, "currentPageNo": PageNo},getJSON);
		function getJSON(data){	
			$("#after_right").html('');
			$("#after_buttom").html('');
			if(data!=null){
				for (var i = 0; i < data.list.length; i++) {
					
				var sdiv="<div class='thumbnail'><img src='/statics/imagesTow/"+data.list[i].dishPicture +"'><h4>"+ data.list[i].dish+":"+ data.list[i].dishPrice +""+'元'+"</h4>"
					+"<p><a onclick='addArr("+ data.list[i].menuId +",\""+ data.list[i].dish +"\","+ data.list[i].dishPrice +")'  class='btn btn-primary' role='button'>提交</a>"
					+"<button type='button' class='btn btn-default' data-container='body' data-toggle='popover' data-placement='top' data-content='Vivamus sagittis lacus vel augue laoreet rutrum faucibus.'>"
					+"查看</button></p></div> ";
					$("#after_right").append(sdiv);
				}
				if(PageNo>=data.totalPageCount){
					PageNo=data.totalPageCount-1;
				}
					var butdiv="<nav aria-label='...'><ul class='pager'><li><a onclick='getdishes("+TypeId+","+(PageNo-1)+")'>上一页</a></li>" +
					data.currentPageNo+"页" +"<li><a onclick='getdishes("+TypeId+","+(PageNo+1)+")'>下一页</a></li></ul></nav>";
					$("#after_buttom").append(butdiv);
			}
		}
}

/*
 * 搜索框的模糊查询
 */
function getSub_dishes(){
	var $text=$("#sub_ss").val();
	if($text !=null & $text !=''){
	$.getJSON('/dishes/getInDishesList',{"InName":$text},function (data){
		if(data!=null){
		$("#after_right").html('');
		$("#after_buttom").html('');
		for(var i = 0; i < data.length; i++){

			var Indiv="<div class='thumbnail'><img src='/statics/imagesTow/"+data[i].dishPicture +"'><h4>"+ data[i].dish+":"+ data[i].dishPrice +""+'元'+"</h4>"
			+"<p><a href='#' class='btn btn-primary' role='button'>添加</a>"
			+"<button type='button' class='btn btn-default' data-container='body' data-toggle='popover' data-placement='top' data-content='Vivamus sagittis lacus vel augue laoreet rutrum faucibus.'>"
			+"查看</button></p></div> ";
			$("#after_right").append(Indiv);
		}
		}else{
	     alert("未能查到能输入的相关内容！");
		}
	});
	}else{
		alert("请输入值，才能查询！");
	}
}
/*
 * 获取保存的的用户list值
 */
function getSetList(){
	$.getJSON('/getDishesList',function (data){
		if(data!=null){
		$("#after_right").html('');
		$("#after_buttom").html('');
		for(var i = 0; i < data.length; i++){
			var Indiv="<div class='thumbnail'><img src='/statics/imagesTow/"+data[i].dishPicture +"'><h4>"+ data[i].dish+":"+ data[i].dishPrice +""+'元'+"</h4><p></div>";
			$("#after_right").append(Indiv);
		}
		}else{
	     alert("未能查到能输入的相关内容！");
		}
	});
}
	